<?php
// +----------------------------------------------------------------------
// | 收藏
// +----------------------------------------------------------------------
// | Copyright (c) 2015-2022 http://www.yicmf.com, All rights reserved.
// +----------------------------------------------------------------------
// | Author: 微尘 <yicmf@qq.com>
// +----------------------------------------------------------------------

namespace app\controller\api\v3;


use app\event\store\Cart as CartEvent;
use app\model\store\Address as AddressesModel;

class Cart extends Common
{

    /**
     * 我的购物车
     */
    public function gets()
    {
        $event = new CartEvent();
        $data = $event->lists($this->user,'app');
        $data['data']['address'] = AddressesModel::where('user_id',$this->user['id'])->where('status',1)->order('is_default DESC')->find();
//			dump($data['data']['store_buy']['goods']);
//			$data['data']['all_number'] = $this->user->carts()->where('status', 1)->where('is_select', 1)->sum('number');;
//			$data['data']['all_price'] = $this->user->carts()->where('status', 1)->where('is_select', 1)->sum('price*number');;
//			if ($data['data']['all_number']) {
//				$data['data']['all_select'] = $this->user->carts()->where('status', 1)->where('is_select', 0)->find() ? 0 : 1;
//			} else {
//				$data['data']['all_select'] = 0;
//			}
        return $this->autoAjax($data);
    }
    /**
     * 我的购物车
     */
    public function lists()
    {
        $event = new CartEvent();
        $data = $event->lists($this->user,'new');
        $data['data']['address'] = AddressesModel::where('user_id',$this->user['id'])->where('status',1)->order('is_default DESC')->find();
//			dump($data['data']['store_buy']['goods']);
//			$data['data']['all_number'] = $this->user->carts()->where('status', 1)->where('is_select', 1)->sum('number');;
//			$data['data']['all_price'] = $this->user->carts()->where('status', 1)->where('is_select', 1)->sum('price*number');;
//			if ($data['data']['all_number']) {
//				$data['data']['all_select'] = $this->user->carts()->where('status', 1)->where('is_select', 0)->find() ? 0 : 1;
//			} else {
//				$data['data']['all_select'] = 0;
//			}
        return $this->autoAjax($data);
    }
    /**
     * 我的购物车
     */
    public function sysnc()
    {

        $event = new CartEvent();
        $data = $event->sysnc($this->request->param(),$this->user,'app');
        $data['data']['address'] = AddressesModel::where('user_id',$this->user['id'])->where('status',1)->order('is_default DESC')->find();
//			dump($data['data']['store_buy']['goods']);
//			$data['data']['all_number'] = $this->user->carts()->where('status', 1)->where('is_select', 1)->sum('number');;
//			$data['data']['all_price'] = $this->user->carts()->where('status', 1)->where('is_select', 1)->sum('price*number');;
//			if ($data['data']['all_number']) {
//				$data['data']['all_select'] = $this->user->carts()->where('status', 1)->where('is_select', 0)->find() ? 0 : 1;
//			} else {
//				$data['data']['all_select'] = 0;
//			}
        return $this->autoAjax($data);
    }
    /**
     * 清空失效商品
     */
    public function clearf()
    {
        $event = new CartEvent();
        $data = $event->clearf($this->user);
//			dump($data['data']['store_buy']['goods']);
//			$data['data']['all_number'] = $this->user->carts()->where('status', 1)->where('is_select', 1)->sum('number');;
//			$data['data']['all_price'] = $this->user->carts()->where('status', 1)->where('is_select', 1)->sum('price*number');;
//			if ($data['data']['all_number']) {
//				$data['data']['all_select'] = $this->user->carts()->where('status', 1)->where('is_select', 0)->find() ? 0 : 1;
//			} else {
//				$data['data']['all_select'] = 0;
//			}
        return $this->autoAjax($data);
    }

    public function add()
    {
        if ($this->request->isPost()) {
            $event = new CartEvent();
            $data = $event->add($this->request->post(), $this->user);
        } else {
            $data['code'] = 1;
            $data['message'] = '请求错误';
        }
        return json($data);
    }

    public function cancel()
    {
        if ($this->request->isPost()) {
            $event = new CartEvent();
            $data = $event->cancel($this->request->post(), $this->user);
        } else {
            $data['code'] = 1;
            $data['message'] = '请求错误';
        }
        return json($data);
    }

    public function collection()
    {
        if ($this->request->isPost()) {
            $event = new CartEvent();
            $data = $event->collection($this->request->post(), $this->user);
        } else {
            $data['code'] = 1;
            $data['message'] = '请求错误';
        }
        return json($data);
    }


    /**
     * 购物车勾选
     */
    public function check()
    {
        if ($this->request->isPost()) {
//				$event = new CartEvent();
//				$data = $event->check($this->request->post(), $this->user);

            $data['code'] = 0;
            if ($this->request->param('id'))
            {
                $this->user->carts()->where('status',1)
                    ->where('id',$this->request->param('id'))
                    ->update(['is_select'=>$this->request->param('is_select')]);
            }

            $event = new CartEvent();
            $data = $event->lists($this->user,'app');

        } else {
            $data['code'] = 1;
            $data['message'] = '请求错误';
        }
        return json($data);
    }

    /**
     * 购物车勾选
     */
    public function checkAll()
    {
        if ($this->request->isPost()) {
//				$event = new CartEvent();
//				$data = $event->check($this->request->post(), $this->user);

            $data['code'] = 0;
            if (1 == $this->request->param('is_select'))
            {
                $this->user->carts()->where('status',1)
                    ->update(['is_select'=>1]);
            }else{
                $this->user->carts()->where('status',1)
                    ->update(['is_select'=>0]);
            }

            $event = new CartEvent();
            $data = $event->lists($this->user,'app');

        } else {
            $data['code'] = 1;
            $data['message'] = '请求错误';
        }
        return json($data);
    }

    /**
     * 购物车勾选
     */
    public function number()
    {
        if ($this->request->isPost()) {
            $event = new CartEvent();
            $data = $event->number($this->request->post(), $this->user);
            $data['data']['all_number'] = $this->user->carts()->where('status', 1)->where('is_select', 1)->sum('number');;
            $data['data']['all_price'] = $this->user->carts()->where('status', 1)->where('is_select', 1)->sum('price*number');;
            if ($data['data']['all_number']) {
                $data['data']['all_select'] = $this->user->carts()->where('status', 1)->where('is_select', 0)->find() ? 0 : 1;
            } else {
                $data['data']['all_select'] = 0;
            }

        } else {
            $data['code'] = 1;
            $data['message'] = '请求错误';
        }
        return json($data);
    }

    /**
     * 删除购物车
     */
    public function delete()
    {
        //   删除选中
        $re = $this->user->carts()->where('is_select', 1)->delete();
        if ($re) {
            $data['code'] = 0;
            $data['data'] = 1;
        } else {
            $data['code'] = 1;
            $data['message'] = '暂无选中商品';
        }
        return json($data);
    }
}
